package com.cyou.nad.tag.table;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cyou.nad.datastore.DataList;
import com.cyou.nad.datastore.exception.InvalidRowColException;



/**
 * table 迭代标签的 单元个标签，在row 标签内部
 * @author lzm 
 */
public class ColTag extends SimpleTagSupport {
	private Log log = LogFactory.getLog(getClass());

	/**
	 * 列对应的sql col name  
	 */
	private String name;

	/**
	 * 对列字段进行可格式化，目前支持 SimpleDateFormart 和 decimalFormat 两种格式化，
	 */
	private String format;
	
	/**
	 * 是否需要对列进行转义 
	 */
	private boolean trans=true;

	@SuppressWarnings("unchecked")
	@Override
	public void doTag() throws JspException, IOException {
		if (StringUtils.isBlank(name))
			throw new JspException("ColTag 没有 colName ");
		Object obj = getJspContext().getAttribute(RowTag.DATA_LIST_DATA, PageContext.PAGE_SCOPE);
		Object obj2 = getJspContext().getAttribute(RowTag.DATA_LIST_CURRENT_ROW, PageContext.PAGE_SCOPE);
		if (obj == null || obj2 == null)
			throw new JspException("ColTag 必须包含在RowTag 中");
		Integer row = (Integer) obj2;
		DataList dataList = (DataList) obj;
		try {
			obj = dataList.getObject(row, name);
			if(obj==null){
				getJspContext().getOut().print("&nbsp;");
				return ;
			}
		} catch (InvalidRowColException e) {
			e.printStackTrace();
			throw new JspException("ColTag 获得数据出错 不正确的行列值", e);
		}
		
		Map<String, Map<String,String>> map = (Map<String, Map<String, String>>) getJspContext().getAttribute(TableTag.TABLE_TAG_TRANS);
		if(map!=null&&trans){
			Map<String, String> nameValuePair = map.get(name);
			if(nameValuePair!=null){
				String str =nameValuePair.get(obj.toString());
				if(str!=null){
					getJspContext().getOut().print(str);
					return ;
				}
			}
		}
		
		try {
			if (StringUtils.isNotBlank(format)) {
				if (obj instanceof Date)
					obj = DateFormatUtils.format((Date) obj, format);
				else if (obj instanceof Number) {
					DecimalFormat decimalFormat = new DecimalFormat(format);
					obj = decimalFormat.format(obj);
				}
			} else {
				if (obj instanceof Date)
					obj = DateFormatUtils.format((Date) obj, "yyyy-MM-dd HH:mm:ss");
			}
		} catch (RuntimeException e) {
			log.warn("格式化数据出错", e);
		}
		if(obj==null){
			getJspContext().getOut().print("&nbsp;");
			return ;
		}
		getJspContext().getOut().print(obj.toString());
	}

	/**
	 * 对列字段进行可格式化，目前支持 SimpleDateFormart 和 decimalFormat 两种格式化，
	 */
	public void setFormat(String format) {
		this.format = format;
	}

	public String getName() {
		return name;
	}
	/**
	 * 列对应的sql col name
	 */
	public void setName(String name) {
		this.name = name;
	}

	public String getFormat() {
		return format;
	}

	public boolean isTrans() {
		return trans;
	}

	public void setTrans(boolean trans) {
		this.trans = trans;
	}

}
